//
//  ZSY_DestinationScrollView.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 16/1/12.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "ZSY_DestinationScrollView.h"

@interface ZSY_DestinationScrollView ()<UIScrollViewDelegate>

@property (nonatomic, retain) ZSY_BaseScrollView *scrollView;
@property (nonatomic, retain) ZSY_BaseImageView *imageView;

@property (nonatomic, assign) NSInteger flag;// 记录当前页码

@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger number;


@end

@implementation ZSY_DestinationScrollView


- (void)dealloc {
    [_scrollView release];
    [_imageView release];
    [_pageControl release];
    [_myTimer release];
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setAllArray:(NSMutableArray<ZSY_DesScrollModel *> *)allArray {
    
    if (_allArray != allArray) {
        [_allArray release];
        _allArray = [allArray retain];
    }
    
    [self createSubviews];
    
}


- (void)createSubviews {
    self.scrollView = [[ZSY_BaseScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    self.scrollView.backgroundColor = [UIColor lightGrayColor];
    
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * [self.allArray count], self.frame.size.height);
    
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    
    [self addSubview:self.scrollView];
    [_scrollView release];
    
    for (int i = 0; i < self.allArray.count; i++) {
        
        self.imageView = [[ZSY_BaseImageView alloc] initWithFrame:CGRectMake((WIDTH * i) + WIDTH, 0, WIDTH, 200)];
        
        NSURL *url = [NSURL URLWithString:_allArray[i].image_url];
        
        [self.imageView sd_setImageWithURL:url placeholderImage:nil];
        
        self.imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress)];
        [self.imageView addGestureRecognizer:singleTap];
        
        
        [self.scrollView addSubview:self.imageView];
        [_imageView release];
    }
    
    // 取最后一张 放最前面
    ZSY_BaseImageView *imageView1 = [[ZSY_BaseImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    NSURL *url1 = [NSURL URLWithString:[_allArray objectAtIndex:self.allArray.count - 1].image_url];
    [imageView1 sd_setImageWithURL:url1 placeholderImage:[UIImage imageNamed:@""]];
    [self.scrollView addSubview:imageView1];
    [imageView1 release];
    // 取第一张 放最后
    ZSY_BaseImageView *imageView2 = [[ZSY_BaseImageView alloc] initWithFrame:CGRectMake(WIDTH * (self.allArray.count + 1), 0, WIDTH, 200)];
    NSURL *url2 = [NSURL URLWithString:[_allArray objectAtIndex:0].image_url];
    [imageView2 sd_setImageWithURL:url2 placeholderImage:[UIImage imageNamed:@""]];
    [self.scrollView addSubview:imageView2];
    [imageView2 release];
    
    id imageFirst = [self.allArray firstObject];
    id imageLast = [self.allArray lastObject];
    
    [self.allArray insertObject:imageLast atIndex:0];
    [self.allArray addObject:imageFirst];
    
    
    // 核心的属性, 设置滚动范围
    self.scrollView.contentSize = CGSizeMake(WIDTH * (self.allArray.count + 2), 200);  // 左右滚
    self.scrollView.contentOffset = CGPointMake(0, 0);
    [self.scrollView scrollRectToVisible:CGRectMake(WIDTH, 0, WIDTH, 200) animated:NO];// 默认从序号1位置放第1页 ，序号0位置位置放第4页
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((WIDTH - 260) / 2, self.scrollView.frame.size.height - 40, 260, 30)];
    self.pageControl.numberOfPages = self.allArray.count - 2;
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self addSubview:self.pageControl];
    [_pageControl release];
    
    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
    //1.记录当前页码,默认为0
    self.index = 0;
    
}


- (void)buttonpress{
    
    self.number = self.pageControl.currentPage + 1;
    ZSY_DesScrollModel *model = self.allArray[self.number];
    
    [self.myDelegate method:model.html_url];
}


-(void)timerAction:(NSTimer *)timer{
    
    //使用scrollview自带的滚动动画,实现起来效果不好,有bug.所以需要自己写动画效果
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.scrollView setContentOffset:CGPointMake((self.index + 1) * self.scrollView.frame.size.width, 0)];
        
        
    }];
    
    self.index++;
    
    //如果滑动到最后一页
    if (self.scrollView.contentOffset.x == (self.allArray.count - 1) * self.scrollView.frame.size.width) {
        //快速移动到第一页
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
        
        self.index = 1;
    }
}

//UIPageControl类型对象中调用的方法
- (void)changePage:(UIPageControl *)page{
    
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.scrollView.frame.size.width, 0) animated:YES];
}

//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.scrollView.frame.size.width) / scrollView.frame.size.width;
    
}

//当手指接触的时候让定时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.myTimer invalidate];
    self.myTimer = nil;
}

//当手指离开的时候开始滑动
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (self.scrollView == scrollView) {
        
        //每次减速结束都让scollView所有的scrollView子视图缩放变为1
        for (UIScrollView * scrollv in scrollView.subviews) {
            if ([scrollv isKindOfClass:[UIScrollView class]]) {
                scrollv.zoomScale = 1.0;
            }
        }
        
        if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * (self.allArray.count - 1)) {
            
            //            NSLog(@"最后一页");//循环滚动
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
        }
        
        if (self.scrollView.contentOffset.x <= 0) {
            
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (self.allArray.count - 2), 0) animated:NO];
        }
        
    }
    
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.scrollView.contentOffset.x / self.scrollView.frame.size.width -1;
    
}


@end
